package com.ruoyi.web.controller.assets;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.OliveAssets;
import com.ruoyi.system.domain.OlivePlantArea;
import com.ruoyi.system.domain.RestAssets;
import com.ruoyi.system.domain.TopFourAsset;
import com.ruoyi.system.service.IOliveAssetsService;
import com.ruoyi.system.service.impl.AssetCategoryMapper;
import com.ruoyi.system.service.impl.PlantAreaMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 基础信息管理Controller
 * 
 * @author hzl
 * @date 2024-12-08
 */
@RestController
@RequestMapping("/assets/assets")
public class OliveAssetsController extends BaseController
{
    @Autowired
    private IOliveAssetsService oliveAssetsService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询基础信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('assets:assets:list')")
    @GetMapping("/list")
    public TableDataInfo list(OliveAssets oliveAssets)
    {
        startPage();
        List<OliveAssets> list = oliveAssetsService.selectOliveAssetsList(oliveAssets);
        return getDataTable(list);
    }

    /**
     * 导出基础信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('assets:assets:export')")
    @Log(title = "基础信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OliveAssets oliveAssets)
    {
        List<OliveAssets> list = oliveAssetsService.selectOliveAssetsList(oliveAssets);
        ExcelUtil<OliveAssets> util = new ExcelUtil<OliveAssets>(OliveAssets.class);
        util.exportExcel(response, list, "基础信息管理数据");
    }

    /**
     * 导入基础信息管理数据
     */
    @Log(title = "资产管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<OliveAssets> util = new ExcelUtil<OliveAssets>(OliveAssets.class);
        List<OliveAssets> assetList = util.importExcel(file.getInputStream());
        LoginUser loginUser =tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = oliveAssetsService.importAssets(assetList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<OliveAssets> util = new ExcelUtil<OliveAssets>(OliveAssets.class);
        return util.importTemplateExcel("资产管理数据");
    }

    /**
     * 获取基础信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('assets:assets:query')")
    @GetMapping(value = "/{assetId}")
    public AjaxResult getInfo(@PathVariable("assetId") String assetId)
    {
        return success(oliveAssetsService.selectOliveAssetsByAssetId(assetId));
    }

    /**
     * 新增基础信息管理
     */
    @PreAuthorize("@ss.hasPermi('assets:assets:add')")
    @Log(title = "基础信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OliveAssets oliveAssets)
    {
        // 从前端表单中提取设备类型、所属种植区和设备购置日期
        String deviceType = AssetCategoryMapper.getAssetCategoryName(Integer.parseInt(oliveAssets.getAssetCategory()));
        String plantAreaId = PlantAreaMapper.getPlantAreaCode(oliveAssets.getPlantAreaId()); // 假设OliveAssets类中有getPlantAreaId方法
        String purchaseDate = String.valueOf(oliveAssets.getAssetPurchaseData());
        // 检查这些字段是否为空或不符合规范
        if (deviceType == null || plantAreaId == null || purchaseDate == null) {
            return AjaxResult.error("设备类型、所属种植区和购置日期不能为空");
        }
        // 生成设备ID，这里假设generateDeviceID方法可以根据设备类型、所属种植区和购置日期生成设备ID
        String assetId = oliveAssetsService.generateDeviceID(deviceType, plantAreaId, purchaseDate);
        if (assetId == null || !oliveAssetsService.validateDeviceID(assetId)) {
            return AjaxResult.error("设备ID不符合规范");
        }

        // 生成完整的设备ID，包括校验码
        String completeDeviceID = oliveAssetsService.generateCompleteDeviceID(assetId);
        oliveAssets.setAssetId(completeDeviceID);
        return toAjax(oliveAssetsService.insertOliveAssets(oliveAssets));
    }

    /**
     * 修改基础信息管理
     */
    @PreAuthorize("@ss.hasPermi('assets:assets:edit')")
    @Log(title = "基础信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OliveAssets oliveAssets)
    {

        return toAjax(oliveAssetsService.updateOliveAssets(oliveAssets));
    }

    /**
     * 删除基础信息管理
     */
    @PreAuthorize("@ss.hasPermi('assets:assets:remove')")
    @Log(title = "基础信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{assetIds}")
    public AjaxResult remove(@PathVariable String[] assetIds)
    {
        return toAjax(oliveAssetsService.deleteOliveAssetsByAssetIds(assetIds));
    }

    // 提供获取前四类资产数据的接口
    @GetMapping("/top-four")
    public List<TopFourAsset> getTopFourAssets() {
        return oliveAssetsService.getTopFourAssets();
    }

    // 提供获取剩余资产数据的接口
    @GetMapping("/rest")
    public RestAssets getRestAssets() {
        return oliveAssetsService.getRestAssets();
    }

    // 根据种植区id获取该种植区前四类资产数据的接口
    @GetMapping("/top-four-by-plant-area")
    public List<TopFourAsset> getTopFourAssetsByPlantArea(OliveAssets oliveAssets) {
        return oliveAssetsService.getTopFourAssetsByPlantArea(oliveAssets);
    }

    // 根据种植区id获取该种植区剩余资产数据的接口
    @GetMapping("/rest-by-plant-area")
    public RestAssets getRestAssetsByPlantArea(OliveAssets oliveAssets) {
        return oliveAssetsService.getRestAssetsByPlantArea(oliveAssets);
    }

}
